<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="工单号" prop="workOrderNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workOrderNumber" placeholder="请填写工单号"  disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="设备编码" prop="equipmentCode"
                :rules="[
                 ]">
              <el-input v-model="inputForm.equipmentCode" placeholder="请填写设备编码"  disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="厂" prop="factory"
                :rules="[
                 ]">
              <el-input v-model="inputForm.factory" placeholder="请填写厂"  disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="区域" prop="area"
                :rules="[
                 ]">
              <el-input v-model="inputForm.area" placeholder="请填写区域" disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="主体设备" prop="mainEquipment"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mainEquipment" placeholder="请填写主体设备"  disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="任务来源" prop="taskSource"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.taskSource" placeholder="请填写任务来源"  disabled=""></el-input> -->
              <el-select v-model="inputForm.taskSource" placeholder="请填写任务来源" style="width:100%;" disabled="">
                <el-option v-for="taskSource in $dictUtils.getDictList('repair_source')" :label="taskSource.label" :value="taskSource.value" :key="taskSource.value" ></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="施工内容" prop="roadworkContent"
                :rules="[
                 ]">
              <el-input v-model="inputForm.roadworkContent" placeholder="请填写施工内容" disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工时" prop="workTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workTime" placeholder="请填写工时"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="状态" prop="state"
                :rules="[
                 ]">
              <el-select v-model="inputForm.state" placeholder="请填状态" style="width:100%;" disabled="">
              <el-option v-for="state in $dictUtils.getDictList('repair_statement')" :label="state.label" :value="state.value" :key="state.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目协办人" prop="projectMan"
                :rules="[
                 ]">
              <user-select :limit='10' :value="inputForm.projectMan" @getValue='(value) => {inputForm.projectMan=value}' @getUserName='getUserName(arguments)' disabled></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
                    <el-form-item label="检修标准工时" prop="overhaulPeople"
                        :rules="[
                         ]">
                      <el-input v-model="inputForm.overhaulPeople" placeholder="请填写检修标准工时" disabled=""></el-input>
                   </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修标准项目承办人数" prop="repirpeople"
                        :rules="[
                         ]">
                      <el-input v-model="inputForm.repirpeople" placeholder="请填写检修标准项目承办人数" disabled=""></el-input>
                   </el-form-item>
                </el-col>
        <el-col :span="12">
            <el-form-item label="完成时间" prop="completeTime"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.completeTime" placeholder="请填写完成时间"  disabled=""></el-input> -->
              <el-date-picker
                    style="width:100%;"
                    v-model="inputForm.completeTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    default-time="12:00:00" disabled="">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="施工方案" prop="repairMake"
                :rules="[
                 ]">
              <el-upload ref="repairMake"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/flowform/form/repairMake`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.repairMake = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.repairMake = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="repairMakeArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="上传人" prop="uploadPeople"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.uploadPeople" placeholder="请填写上传人"     ></el-input> -->
              <user-select :limit='1' :value="inputForm.uploadPeople" @getValue='(value) => {inputForm.uploadPeople=value}' @getUserName='getUserName(arguments)'></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="上传时间" prop="repairMakeTime"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.repairMakeTime" placeholder="请填写上传时间"     ></el-input> -->
              <el-date-picker
                    style="width:100%;"
                    v-model="inputForm.repairMakeTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    default-time="12:00:00">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                  {required: true, message:'备注信息不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col> -->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :disabled="method === 'changestate'"  @click="visible = false">关闭</el-button>
      <el-button :disabled="method === 'changestate'" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
   </el-dialog>
</div>
</template>

<script>
  import UserSelect from '@/components/userSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        repairMakeArra: [],
        inputForm: {
          id: '',
          workOrderNumber: '',
          equipmentCode: '',
          factory: '',
          area: '',
          mainEquipment: '',
          taskSource: '',
          roadworkContent: '',
          completeTime: '',
          repairMake: '',
          uploadPeople: '',
          repairMakeTime: '',
          remarks: '',
          testId: '',
          workTime: '',
          state: '',
          projectMan: '',
          loginName: '',
          checkTime: '',
          checkPeopleNumber: '',
          repirpeople: '',
          overhaulPeople: '',
          processId: ''
        }
      }
    },
    components: {
      UserSelect
    },
    methods: {
      init (method, id, testId) {
        this.method = method
        this.inputForm.id = id
        this.inputForm.testId = testId
        this.$http({
           url: `/sys/log/logId`,
           method: 'get'
         }).then(({data}) => {
          this.loginName = data.logId.roleNames
          // if (this.loginName !== '检修区域班长' && method !== 'view') {
          //   this.$alert('您没有此权限', '提示', {
          //     confirmButtonText: '确定',
          //     type: 'warning'
          //   })
          // } else {
          if (method === 'add') {
            this.title = `新建制定检修检修方案`
          } else if (method === 'edit') {
            this.title = '填报检修方案'
          } else if (method === 'view') {
            this.title = '查看制定检修检修方案'
          }
          this.repairMakeArra = []
          this.visible = true
          this.loading = false
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            if (method === 'edit' || method === 'view' || method === 'changestate') { // 修改或者查看
            console.log(method, id)
              this.loading = true
              this.$http({
                url: `/flowform/form/repairMake/queryById?id=${this.inputForm.id}`,
                method: 'get'
              }).then(({data}) => {
                this.inputForm = this.recover(this.inputForm, data.repairMake)
                  if (method === 'changestate') {
                    this.inputForm.state = 1
                    this.doSubmit()
                  }
                this.inputForm.repairMake.split('|').forEach((item) => {
                  if (item.trim().length > 0) {
                    this.repairMakeArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                  }
                })
                this.loading = false
              })
            }
          })
          // }
          })
      },
      // 表单提交
      doSubmit () {
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'repair_make',
            value: this.inputForm.repairMake
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'make_people',
            value: this.inputForm.uploadPeople
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'repair_make_time',
            value: this.inputForm.repairMakeTime
          }
        })
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/flowform/form/repairMake/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
